package com.mp.tanlong;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Environment;
import android.util.Log;

/**
 * Created by Jay on 2015/9/14 0014.
 */
public class DownLoadThread implements Runnable {

	private String dlUrl;
	private DownloadProgressListener progressListener;
	public DownLoadThread(String dlUrl) {
		this.dlUrl = dlUrl;
	}
	
	public void addProgressListener(DownloadProgressListener l)
	{
		this.progressListener = l;
	}

	@Override
	public void run() {
		Log.e("HEHE", "开始下载~~~~~");
		InputStream in = null;
		FileOutputStream fout = null;
		try {

			URL httpUrl = new URL(dlUrl);
			HttpURLConnection conn = (HttpURLConnection) httpUrl
					.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			in = conn.getInputStream();
			int fullSize = conn.getContentLength();
			File downloadFile, sdFile;
			if (Environment.getExternalStorageState().equals(
					Environment.MEDIA_MOUNTED)) {
				Log.e("HEHE", "SD卡可写");
				downloadFile = Environment.getExternalStorageDirectory();
				sdFile = new File(downloadFile, "csdn_client.apk");
				fout = new FileOutputStream(sdFile);
			} else {
				Log.e("HEHE", "SD卡不存在或者不可读写");
			}
			byte[] buffer = new byte[1024];
			int sumSize = 0;
			int len;
			while ((len = in.read(buffer)) != -1) {
				fout.write(buffer, 0, len);
				sumSize += len;
				float progress = 1.0f * sumSize / fullSize;
				this.progressListener.onProgress(progress);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		Log.e("HEHE", "下载完毕~~~~");
	}
	
	interface DownloadProgressListener
	{
		void onProgress(float progress);
	}
}